import 'package:flutter/material.dart';
import 'package:flutter_eve_plugin/constants/constant.dart';
import 'package:flutter_eve_plugin/model/ye_xiu/checkpoint/dungeon_rank.dart';
import 'package:flutter_eve_plugin/model/ye_xiu/checkpoint/dungeons.dart';
import 'package:flutter_eve_plugin/model/ye_xiu/checkpoint/fight_succ.dart';
import 'package:flutter_eve_plugin/model/ye_xiu/checkpoint/stageinfo.dart';
import 'package:flutter_eve_plugin/model/ye_xiu/checkpoint/stages.dart';
import 'package:flutter_eve_plugin/model/ye_xiu/checkpoint/user_card.dart';
import 'package:flutter_eve_plugin/model/ye_xiu/checkpoint/vitality.dart';
import 'package:flutter_eve_plugin/pages/ye_xiu/checkpoint/constant.dart';
import 'package:flutter_eve_plugin/plugin/main.dart';
import 'package:flutter_eve_plugin/route/main.dart';
import 'package:flutter_eve_plugin/store/base_action.dart';
import 'package:redux/redux.dart';
import 'package:redux_thunk/redux_thunk.dart';

pushSelectMap(Store store, Map<int, String> map) {
  store.dispatch(BaseAction(type: PREPARE_GAME_SELECT_MAP, data: map));
}

updateSelectKey(Store store, int selectKey) {
  store.dispatch(BaseAction(type: PREPARE_GAME_INDEX, data: selectKey));
}

updateDungeonList(Store store, CheckpointDungeons info) {
  store.dispatch(BaseAction(type: CHECKPOINT_DUNGEONS_INFO, data: info));
}

updateStageInfo(Store store, CheckpointStageInfo info) {
  store.dispatch(BaseAction(type: CHECKPOINT_STAGE_INFO, data: info));
}

updateStages(Store store, CheckpointStages info) {
  store.dispatch(BaseAction(type: CHECKPOINT_STAGES, data: info));
}

updateRank(Store store, DungeonRank info) {
  store.dispatch(BaseAction(type: CHECKPOINT_RANK, data: info));
}

updateVitality(Store store, DungeonVitality info) {
  store.dispatch(BaseAction(type: CHECKPOINT_VITALITY, data: info));
}

updateUserCard(Store store, UserCard info) {
  store.dispatch(BaseAction(type: CHECKPOINT_USERCARD, data: info));
}

updateStageID(Store store, String stageid) {
  store.dispatch(BaseAction(type: CHECKPOINT_STAGEID, data: stageid));
}

updateFightSucc(Store store, FightSucc info) {
  store.dispatch(BaseAction(type: CHECKPOINT_FIGHT_SUCC, data: info));
}

ThunkAction dungeonList(int groupid) {
  return (Store store) async {
    CheckpointDungeons info = await PluginEve.sendMessage(
        PluginConstant.httpYXDungeons,
        param: {'groupid': groupid ?? 1});
    updateDungeonList(store, info);
  };
}

ThunkAction stageInfo(String stageid, BuildContext context) {
  return (Store store) async {
    CheckpointStageInfo info = await PluginEve.sendMessage(
        PluginConstant.httpYXDungeonStageInfo,
        param: {'stageid': stageid ?? 0},
        context: context);
    updateStageInfo(store, info);
  };
}

ThunkAction stages(String dungeonid, BuildContext context) {
  return (Store store) async {
    CheckpointStages stages = await PluginEve.sendMessage(
        PluginConstant.httpYXDungeonStages,
        param: {'dungeonid': dungeonid ?? 0},
        context: context);
    updateStages(store, stages);
  };
}

ThunkAction rank(String dungeonid, BuildContext context) {
  return (Store store) async {
    DungeonRank info = await PluginEve.sendMessage(
        PluginConstant.httpYXDungeonRank,
        param: {'dungeonid': dungeonid ?? 0},
        context: context);
    updateRank(store, info);
  };
}

ThunkAction vitality(BuildContext context) {
  return (Store store) async {
    DungeonVitality info = await PluginEve.sendMessage(
        PluginConstant.httpYXDungeonVitality,
        context: context);
    updateVitality(store, info);
  };
}

ThunkAction userCard(int pagenum, BuildContext context) {
  return (Store store) async {
    UserCard userCard = await PluginEve.sendMessage(
        PluginConstant.httpYXUserCard,
        param: {'pagenum': pagenum ?? 1},
        context: context);
    updateUserCard(store, userCard);
  };
}

ThunkAction fight({BuildContext context, String stageId, List<String> team}) {
  return (Store store) async {
    FightSucc fightSucc = await PluginEve.sendMessage(
        PluginConstant.httpYXDungeonFight,
        httpType: HttpType.POST,
        context: context,
        param: {'stageId': stageId ?? '', 'team': team ?? []});
    updateFightSucc(store, fightSucc);
    Navigator.pushNamed(context, RouteConstant.CheckpointStartGamePage);
  };
}
